package com.ultrasoft.farmasoft.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ultrasoft.farmasoft.domain.Bill;
import com.ultrasoft.farmasoft.domain.Client;
import com.ultrasoft.farmasoft.domain.Locality;
import com.ultrasoft.farmasoft.domain.Operation;
import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.Region;
import com.ultrasoft.farmasoft.domain.Transaction;
import com.ultrasoft.farmasoft.domain.User;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.service.BuyProductsTransactionService;
import com.ultrasoft.farmasoft.service.CustomersService;
import com.ultrasoft.farmasoft.util.UtilCommon;
import com.ultrasoft.farmasoft.util.UtilConverter;


@Controller
@SessionAttributes({"s_UserPharmacy"}) 
@RequestMapping(value="/Reports")
public class ReportsEconomicReportsController {

	private HttpSession session;
	private Userpharmacy s_UserPharmacy;
	private int s_userId;
	private int s_pharmacyId;
	
	private List<Pharmacy> listPharmacyEconomicReports;
	
	private final CustomersService customerService;
	private final BuyProductsTransactionService buyProductsTransactionService;

	@Autowired
	public ReportsEconomicReportsController(BuyProductsTransactionService buyProductsTransactionService,CustomersService customerService){
		this.customerService 				= customerService;
		this.buyProductsTransactionService 	= buyProductsTransactionService;
		this.listPharmacyEconomicReports	= new ArrayList<Pharmacy>();
	}
	
	private boolean sessionControl(HttpServletRequest request){
		boolean res = false;
		session = request.getSession(true);
		if(session.getAttribute("s_UserPharmacy") != null){
			s_UserPharmacy 	= (Userpharmacy)session.getAttribute("s_UserPharmacy");
			s_userId 		= s_UserPharmacy.getUser().getUserId();
			s_pharmacyId 	= s_UserPharmacy.getPharmacy().getPharmacyId();
			res = true;
		}
		return res;
	}
	
	private String redirectReportsEconomicReports(HttpServletRequest request){
		if(!sessionControl(request))
			return "redirect:/index"; 
		else
			return "redirect:/economicReports?"+UtilCommon.menuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.menuId_)+"&"+UtilCommon.subMenuId_+"="+UtilConverter.converterStringToShort(request,UtilCommon.subMenuId_); 
	}
	
	@RequestMapping(value="/economicReports", method=RequestMethod.GET)
	public String handleRequestReportsPersonalArching(HttpServletRequest request, Model model) throws ServletException {
		if(this.sessionControl(request)){	
			listPharmacyEconomicReports = customerService.getListPharmacyByPharmacyId(s_pharmacyId,true);
			model.addAttribute("listPharmacy",listPharmacyEconomicReports);
			return UtilCommon.fReports_EconomicReports+"economicReports";
		}else
			return redirectReportsEconomicReports(request);
	}
	
	// 
	
	@RequestMapping(value = "/economicReportsByDateStartByDateEnd", method = RequestMethod.GET)
   	public @ResponseBody List<Transaction> handleRequestLoadTableTempByCodeBar(HttpServletRequest request){			
		List<Transaction> list = new ArrayList<Transaction>();
		Date dateEnd = UtilConverter.converterStringToDate(request,"dateEnd");
		Calendar calendarEnd = Calendar.getInstance();
		calendarEnd.setTime(dateEnd);
		calendarEnd.add(Calendar.DATE,1);		
		List<Transaction> temp = buyProductsTransactionService.getListTransactionByListPharmacyByStartDateByEndDate(listPharmacyEconomicReports,UtilConverter.converterStringToDate(request,"dateStart"),calendarEnd.getTime());
		for (Transaction transaction : temp) {
			list.add(getTransactionTemp(transaction));
		}
		return list;
    }
	
	private Transaction getTransactionTemp(Transaction transaction){
		Transaction t = new Transaction(transaction.getTransactionPK().getPeriodIds(),transaction.getTransactionPK().getTransactionId());
		t.setAmount(transaction.getAmount());
		t.setUnits(transaction.getUnits());
		t.setDateTransaction(new Date(transaction.getDateTransaction().getTime()));
		if(transaction.getOperationId() != null){
			Operation o = new Operation(transaction.getOperationId().getOperationId(),transaction.getOperationId().getName());
			t.setOperationId(o);
		}
		if(transaction.getClientId() != null){
			Pharmacy pharma = new Pharmacy(transaction.getClientId().getPharmacyId().getPharmacyId(),transaction.getClientId().getPharmacyId().getName(),transaction.getClientId().getPharmacyId().getClient());
			Client c = new Client(transaction.getClientId().getClientId(),transaction.getClientId().getClientType(),transaction.getClientId().getName(),transaction.getClientId().getActive());
			c.setNit(transaction.getClientId().getNit());
			c.setPharmacyId(pharma);
			t.setClientId(c);
		}
		if(transaction.getUserpharmacy() != null){
			Userpharmacy up = new Userpharmacy(transaction.getUserpharmacy().getUserpharmacyPK().getPharmacyId(),transaction.getUserpharmacy().getUserpharmacyPK().getUserId(),transaction.getUserpharmacy().getActive());
			if(transaction.getUserpharmacy().getPharmacy() != null){
				Pharmacy pharma = new Pharmacy(transaction.getUserpharmacy().getPharmacy().getPharmacyId(),transaction.getUserpharmacy().getPharmacy().getName(),transaction.getUserpharmacy().getPharmacy().getClient());
				if(transaction.getUserpharmacy().getPharmacy().getLocality() != null){
					Locality locality = new Locality(transaction.getUserpharmacy().getPharmacy().getLocality().getLocalityPK().getRegionId(),transaction.getUserpharmacy().getPharmacy().getLocality().getLocalityPK().getLocalityId());
					locality.setName(transaction.getUserpharmacy().getPharmacy().getLocality().getName());
					if(transaction.getUserpharmacy().getPharmacy().getLocality().getRegion() != null){
						Region r = new Region(transaction.getUserpharmacy().getPharmacy().getLocality().getRegion().getRegionId());
						r.setName(transaction.getUserpharmacy().getPharmacy().getLocality().getRegion().getName());
						locality.setRegion(r);
					}					
					pharma.setLocality(locality);
				}
				up.setPharmacy(pharma);
			}
			if(transaction.getUserpharmacy().getUser() != null){
				User u = new User(transaction.getUserpharmacy().getUser().getUserId());
				u.setName(transaction.getUserpharmacy().getUser().getName());
				u.setLastName(transaction.getUserpharmacy().getUser().getLastName());
				u.setLastName2(transaction.getUserpharmacy().getUser().getLastName2());
				u.setCi(transaction.getUserpharmacy().getUser().getCi());
				up.setUser(u);
			}
			t.setUserpharmacy(up);
		}
		if(transaction.getBill() != null){
			Bill b = new Bill(transaction.getBill().getBillPK().getPeriodId(),transaction.getBill().getBillPK().getBillId());
			b.setActive(transaction.getBill().getActive());
			b.setAmount(transaction.getBill().getAmount());
			b.setBillNum(transaction.getBill().getBillNum());
			b.setClient(transaction.getBill().getClient());
			b.setControlCode(transaction.getBill().getControlCode());
			b.setDate(transaction.getBill().getDate());
			b.setNit(transaction.getBill().getNit());
			t.setBill(b);
		}
		//Bill b = new Bill(transaction.getBill().getBillPK().getPeriodId(),transaction.getBill().getBillPK().getBillId());
		//b.set
		return t;
	}
}
